package com.hzncc.scaffold.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.Ask;
import com.hzncc.scaffold.entity.Bo.AskBo;
import com.hzncc.scaffold.entity.Other.User;
import com.hzncc.scaffold.entity.Vo.AskVo;
import com.hzncc.scaffold.entity.Vo.CourseVo;
import com.hzncc.scaffold.entity.Vo.PublisherVo;
import com.hzncc.scaffold.mapper.AskMapper;
import com.hzncc.scaffold.service.AskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzncc.scaffold.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 提问问题表 服务实现类
 * </p>
 *
 * @author DrunkBear
 * @since 2022-04-27
 */
@Service
public class AskServiceImpl extends ServiceImpl<AskMapper, Ask> implements AskService {

    @Autowired
    AskMapper askMapper;

    @Autowired
    UserService userService;

    @Override
    public ResultT list(Integer pageNum, Integer pageSize, String keyword, Integer courseId, Integer status) {
        QueryWrapper<AskBo> qw = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(keyword)) {
            qw.like("`ask`.`title`", keyword);
        }
        if (courseId != null) {
            qw.eq("`course`.`id`", courseId);
        }
        if (status != null) {
            qw.eq("`ask`.`status`", status);
        }

        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<AskBo> askBoList = askMapper.list(qw);


        ArrayList<AskVo> askVoList = new ArrayList<>();

        for (AskBo askBo : askBoList) {

            AskVo askVo = new AskVo();
            User creator = new User();
            CourseVo courseVo = new CourseVo();
            PublisherVo publisherVo = new PublisherVo();

            if (userService.getById(askBo.getCreatorId()) != null) {
                creator = userService.getById(askBo.getCreatorId());
            }
            if (askBo.getCourseId() != null) {
                courseVo.setId(askBo.getCourseId());
            }
            if (StrUtil.isNotEmpty(askBo.getCourseTitle())) {
                courseVo.setTitle(askBo.getCourseTitle());
            }
            if (askBo.getPublisherId() != null) {
                publisherVo.setId(askBo.getPublisherId());
            }

            if (StrUtil.isNotEmpty(askBo.getPublisherName())) {
                publisherVo.setName(askBo.getPublisherName());
            }

            BeanUtil.copyProperties(askBo, askVo);
            askVo.setPublisher(publisherVo);
            askVo.setCourse(courseVo);
            askVo.setCreator(creator);
            askVoList.add(askVo);
        }

        PageInfo<AskVo> info = new PageInfo<>();
        info.setList(askVoList);
        info.setTotal(new PageInfo<>(askBoList).getTotal());

        return ResultT.success(info);
    }
}
